import React, { Component } from 'react';
import { Form } from 'antd';
import StaticEditTable from '../../Common/StaticEditTable/StaticEditTable';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabExamineComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      formData: props.formData, /* 配置 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sId: props.sId, /* 业务数据Id */
      checkConfig: [], /* 审核信息表配置 */
      headerConfig: [], /* 表头配置 */
    };
  }

  componentWillMount() {
    /* 获取表数据 */
    const {
      masterConfig, formData, sActiveId, sId, sTabId, componentName,
    } = this.state;
    if (masterConfig.sId !== '') {
      const { dispatch } = this.props;
      const checkTable = formData.filter(item => (item.sTbName.toLowerCase() === 'sysbillcheckresult'))[0];
      const checkConfig = checkTable.gdsconfigformslave;
      /* 主表配置 */
      if (sId !== undefined && sId !== '') {
        const checksId = masterConfig.sId;
        /* 表Id */
        const value = {
          sSqlCondition: {
            sParentId: sId,
          },
        };
        const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${checksId}?sModelsId=${sActiveId}`;
        dispatch({
          type: 'content/getUrlPostData',
          payload: {
            url: dataUrl, sTabId, componentName, value, formRoute: '/sysbillcheckresult', returnType: 'data',
          },
        });
      }
      /* 获取表头 */
      const headerConfig = [];
      for (const child of checkConfig) {
        if (child.sName !== '' && child.bVisible && child.showName !== '') {
          headerConfig.push({
            title: child.showName,
            dataIndex: child.sName,
            width: child.iFitWidth,
          });
        }
      }
      this.setState({ headerConfig, checkConfig });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { disabled, content } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, loading, componentName,
    } = dataList;
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        if (componentName === 'sysbillcheckresult') {
          this.setState({ disabled });
        }
      }
    }
    this.setState({ disabled });
  }

  shouldComponentUpdate(nextProps) {
    const { componentName } = nextProps;
    let flag = true;
    if (nextProps.disabled !== this.state.disabled) {
      return true;
    }
    /* 弹窗数据回显需要刷新列表 */
    if (nextProps.refreshList) {
      return true;
    }
    if (nextProps.disabled === this.state.disabled && nextProps.disabled === false) {
      return false;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  /* 用于返回列表编辑框弹窗所属行主键 */
  handleRecordKey = (key) => {
    this.props.handleRecordKey({ checkDataKey: key });
  }

  render() {
    const {
      app, form, handleCheckData, dataExist, dataName, dataType, handleModalPane, dataList, onClose,
    } = this.props;
    const {
      headerConfig, disabled, checkConfig, sActiveId,
    } = this.state;
    const { handleRecordKey } = this;
    const staticEditProps = {
      sActiveId,
      showConfigList: checkConfig,
      headerConfig,
      dataListAll: dataExist ? dataList : [],
      disabled,
      handleTableData: handleCheckData,
      handleRecordKey, /* 用于返回列表编辑框弹窗所属行主键 */
      app,
      form,
      dataName,
      dataType,
      handleModalPane,
      btnAddShow: false,
      btnDelShow: false,
      onClose,
    };
    return (
      <FormItem className={styles.subForm}>
        <StaticEditTable {...staticEditProps} />
      </FormItem>
    );
  }
}

export default TabExamineComponent;
